﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Fuse8.DomainFramework.Common.Pipeline
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SimplePipeline<T>
    {
        private readonly IList<Func<IList<T>, IList<T>>> _operations = new List<Func<IList<T>, IList<T>>>();

        /// <summary>
        /// 
        /// </summary>
        protected IList<Func<IList<T>, IList<T>>> Operations
        {
            get
            {   
                return _operations;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="operation"></param>
        /// <returns></returns>
        public SimplePipeline<T> Register(Func<IList<T>, IList<T>> operation)
        {
            Guard.ArgumentNotNull(operation, "operation");
            
            Operations.Add(operation);

            return this;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable<T> ReturnChain()
        {
            IList<T> current = new List<T>();

            foreach (var operation in Operations)
            {
                current = operation(current);
            }

            return current;
        }
    }
}
